package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;



    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        statement.setActualArrivalTime(new Date());
        Statement obj = statementMapper.selectByPrimaryKey(statement.getId());
        if (Statement.STATUS_PAID.equals(obj.getStatus())) {
            throw new BusinessException("已支付不能修改");
        }
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateAmount(Long id, BigDecimal totalQuantity, BigDecimal totalAmount, BigDecimal discountAmount) {
        statementMapper.updateAmount(id,totalQuantity,totalAmount,discountAmount);
    }

    @Override
    public Statement queryByAppointmentId(Long appointmentId) {

        return statementMapper.selectByAppointmentId(appointmentId);
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        Statement statement = new Statement();
        statement.setAppointmentId(appointmentId);

        //到店--创建结算单
        Appointment appointment = appointmentService.get(appointmentId);
        statement.setCustomerName(appointment.getCustomerName());
        statement.setCustomerPhone(appointment.getCustomerPhone());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());
        statement.setLicensePlate(appointment.getLicensePlate());
        statement.setCarSeries(appointment.getCarSeries());
        statement.setInfo(appointment.getInfo());
        statement.setServiceType(appointment.getServiceType() + 0L);
        statement.setCreateTime(new Date());
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
        //修改预约状态：结算单生成
        appointmentService.changeStatus(appointmentId, Appointment.STATUS_SETTLE);
        return statement;
    }


}
